package org.stvd.entities.admin;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.stvd.core.annotation.serializer.DateTimeSerializer;
import org.stvd.entities.base.BaseEntity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * 系统用户-实体类
 * @author houzx
 */
@Entity
@Table(name="users")
public class Users extends BaseEntity{
    
    private static final long serialVersionUID = 3205348626676688839L;
    
    /**
     * 用户ID
     */
    @Id
    @Column(name="user_id",nullable=false)
    private String userId = "";
    
    /**
     * 用户名称
     */
    @Column(name="uname",nullable=false)
    @NotEmpty(message="用户名称不能为空！")
    private String uname = "";
    
    /**
     * 账户密码
     */
    @Column(name="pwd",nullable=false)
    @NotEmpty(message="密码不能为空！")
    @JsonIgnore
    private String pwd = "";
    
    /**
     * 确认密码
     */
    @Transient
    @JsonIgnore
    private String pwdConfirm = "";
    
    /**
     * 旧密码
     */
    @Transient
    @JsonIgnore
    private String oldPwd = "" ;
    
    /**
     * 是否启用{T:启用;F:禁用}
     */
    @Column(name="enable",nullable=false)
    private String enable = "";
    
    /**
     * 注册时间
     */
    @Column(name="indate", nullable =false)    
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonSerialize(using = DateTimeSerializer.class)
    private Date indate = null;
    
    /**
     * 修改时间
     */
    @Column(name="modifydate")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonSerialize(using = DateTimeSerializer.class)
    private Date modifyDate = null;
    
    /**
     * 最后登录时间
     */
    @Column(name="lastdate")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonSerialize(using = DateTimeSerializer.class)
    @JsonIgnore
    private Date lastDate = null;
    
    /**
     * 登录时长
     */
    @Column(name="logintimes")   
    @JsonIgnore
    private Integer logintimes=0;
    
    @Transient
    @JsonIgnore
    private UserBase userBase = null;
    @Transient
    @JsonIgnore
    private List<Department> departmentList=new ArrayList<Department>();
    @Transient
    @JsonIgnore
    private List<Roles> roleList = new ArrayList<Roles>();
    @Transient
    @JsonIgnore
    private List<Guids> guidList = new ArrayList<Guids>();
    @Transient
    @JsonIgnore
    private List<UserLogin> userLoginList=new ArrayList<UserLogin>();
    @Transient
    @JsonIgnore
    private String roleId = "" ;
    @Transient
    @JsonIgnore
    private String depId = "";
    @Transient
    @JsonIgnore
    private String guid = "";
    @Transient
    @JsonIgnore
    private String loginAccount = "";
    @Transient
    @JsonIgnore
    private String accountType = "";
    
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getPwdConfirm() {
        return pwdConfirm;
    }
    public void setPwdConfirm(String pwdConfirm) {
        this.pwdConfirm = pwdConfirm;
    }
    public String getOldPwd() {
        return oldPwd;
    }
    public void setOldPwd(String oldPwd) {
        this.oldPwd = oldPwd;
    }
    public String getEnable() {
        return enable;
    }
    public void setEnable(String enable) {
        this.enable = enable;
    }
    public Date getIndate() {
        return indate;
    }
    public void setIndate(Date indate) {
        this.indate = indate;
    }
    public Date getModifyDate() {
        return modifyDate;
    }
    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }
    public Date getLastDate() {
        return lastDate;
    }
    public void setLastDate(Date lastDate) {
        this.lastDate = lastDate;
    }
    public Integer getLogintimes() {
        return logintimes;
    }
    public void setLogintimes(Integer logintimes) {
        this.logintimes = logintimes;
    }
    public String getLoginAccount() {
        return loginAccount;
    }
    public void setLoginAccount(String loginAccount) {
        this.loginAccount = loginAccount;
    }
    public String getAccountType() {
        return accountType;
    }
    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }
    public UserBase getUserBase() {
        return userBase;
    }
    public void setUserBase(UserBase userBase) {
        this.userBase = userBase;
    }
    public List<Roles> getRoleList() {
        return this.roleList;
    }
    public void setRoleList(List<Roles> roleList) {
        this.roleList = roleList;
    }
    
    public List<Department> getDepartmentList() {
        return this.departmentList;
    }
    public void setDepartmentList(List<Department> departmentList) {
        this.departmentList = departmentList;
    }
    
    public List<Guids> getGuidList() {
        return guidList;
    }
    public void setGuidList(List<Guids> guidList) {
        this.guidList = guidList;
    }
    public List<UserLogin> getUserLoginList() {
        return userLoginList;
    }
    public void setUserLoginList(List<UserLogin> userLoginList) {
        this.userLoginList = userLoginList;
    }
    
    public String getDepId() {
        if(depId != null && !depId.equals(""))
            return depId;
        if(departmentList!=null) {
            for(Department department : departmentList){
                if(depId == null || depId.equals("")){
                    depId = department.getDepId();
                } else {
                    depId += "," + department.getDepId();
                }
            }
        }
        return depId;
    }
    public void setDepId(String depId) {
        this.depId = depId;
    }
    
    public String getRoleId() {
        if(roleId != null && !"".equals(roleId))
            return roleId;
        if(roleList!=null) {
            for(Roles role : roleList){
                if("".equals(roleId)){
                    roleId = role.getRoleId();
                } else {
                    roleId += "," + role.getRoleId();
                }
            }
        }
        return roleId;
    }
    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }
    
    public String getGuid() {
        if(guid != null && !"".equals(guid))
            return guid;
        if(guidList!=null) {
            for(Guids guids: guidList){
                if("".equals(guid)){
                    guid = guids.getGuid();
                } else {
                    guid += "," + guids.getGuid();
                }
            }
        }
        return guid;
    }
    
    public void setGuid(String guid) {
        this.guid = guid;
    }
    
    
    @Transient
    @JsonIgnore
    public String getRoleNames() {
        String roleNames = "";
        Iterator<Roles> var3 = this.roleList.iterator();
        while (var3.hasNext()) {
            Roles role = (Roles) var3.next();
            if ("".equals(roleNames)) {
                roleNames = role.getRoleName();
            } else {
                roleNames = roleNames + "," + role.getRoleName();
            }
        }
        return roleNames;
    }

    @Transient
    @JsonIgnore
    public String getDepNames() {
        String depNames = "";
        Iterator<Department> var3 = this.departmentList.iterator();
        while (var3.hasNext()) {
            Department department = (Department) var3.next();
            if ("".equals(depNames)) {
                depNames = department.getDepName();
            } else {
                depNames = depNames + "," + department.getDepName();
            }
        }
        return depNames;
    }
}
